#pragma once 
#include<iostream>
#include<string>
#include<fstream>
#include<unordered_map>
#include"Log.hpp"
using namespace log_ns;

const static std::string sep=": ";
class Dict
{
private:
    void LoadDict (const std::string &path)
    {
         std::ifstream in (path);//使用信息流打开文件
         
        if(!in.is_open()) 
        {   
            LOG(FATAL,"open  %s false",path.c_str());
            exit(1);
        }
        std::string line; 
        while(std::getline(in,line))
        {
            LOG(DEBUG,"load info :%s , success\n",line.c_str());
            if(line.empty())continue;
            auto pos=line.find(sep);//找到“ :“的下标//如果找不到会返回一个无限大
            if(pos==std::string ::npos)continue;//
            
            std::string key=line.substr(0,pos);
            std::string value=line.substr(pos+sep.size());
            if(value.empty())
            {
                continue;
            }
            _dict.insert(std::make_pair(key,value));
        }
        LOG(INFO,"load %s done\n",path.c_str());

        in.close();

    }
   
public:
    Dict(const std::string &path):_dict_path(path)//这个函数的目的是将文件的key和value传递到unordered_map中
    {
        LoadDict(_dict_path);
       
    }
    std::string Translate(std::string word)
    {
        if(word.empty()) return "None";
        auto iter=_dict.find(word);
        if(iter==_dict.end())return "None";
        else return iter->second;
        
    }
    ~Dict()
    {

    }
private:
   std::unordered_map<std::string,std::string>_dict;
   std::string _dict_path;

};

